package com.vcredit.creditcat.service;

import com.vcredit.creditcat.framework.pagination.Criteria;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.model.AppCondition;
import com.vcredit.creditcat.persistence.base.AppConditionMapper;
import com.vcredit.creditcat.persistence.base.ProcessSpecialConditionMapper;
import com.vcredit.creditcat.persistence.base.ProductConditionMapper;
import com.vcredit.creditcat.vo.AppConditionParamVo;
import com.vcredit.creditcat.vo.AppConditionVo;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * @author zhangxingong
 * @version 1.0
 * @since 1.0
*/
@Service
public class AppConditionService {

  @Autowired
  private AppConditionMapper appConditionMapper;
  @Autowired
  private ProductConditionMapper productConditionMapper;
  @Autowired
  private ProcessSpecialConditionMapper processSpecialConditionMapper;

  public Page<AppCondition> findByPage(Criteria criteria) {
    return new Page<AppCondition>(appConditionMapper.findByPageCount(criteria),
        appConditionMapper.findByPage(criteria));
  }

  public List<AppCondition> find(AppCondition appCondition) {
    return appConditionMapper.find(appCondition);
  }

  public AppCondition findById(Long appConditionId) {
    return appConditionMapper.findById(appConditionId);
  }

  public List<AppCondition> findAll() {
    return appConditionMapper.findAll();
  }

  public void save(AppConditionVo appConditionVo) {
    try {
      AppCondition appCondition = new AppCondition();
      BeanUtils.copyProperties(appCondition, appConditionVo);
      appConditionMapper.insert(appCondition);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void update(AppCondition appCondition) {
    appConditionMapper.update(appCondition);
  }

  public void removeAppCondition(String ids) {
    for (String appConditionIdString : ids.split(",")) {
      Long appConditionId = Long.parseLong(appConditionIdString);
      appConditionMapper.delete(appConditionId);
    }
  }

  public void batchInsert(AppConditionParamVo paramVo) {
    int type = Integer.parseInt(paramVo.getType());
    switch (type) {
      case 1: //t_app_version
        appConditionMapper.batchInsert(paramVo);
        break;
      case 2: //t_banner
        appConditionMapper.batchInsert(paramVo);
        break;
      case 3: //t_product_condition
        productConditionMapper.batchInsert(paramVo);
        break;
      case 4: //t_spercial_product
        processSpecialConditionMapper.batchInsert(paramVo);
        break;
      default:
    }
  }
}
